package com.clkj.cldisabledmanclient.mvp.recoverorg;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.jaylv.JayLvBaseAdapter;
import com.clkj.cldisabledmanclient.adapter.jaylv.ViewHolder;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.bean.MyRecoverOrgBean;
import com.clkj.cldisabledmanclient.constant.Config;
import com.clkj.cldisabledmanclient.constant.ShareKey;
import com.clkj.cldisabledmanclient.constant.TipStr;
import com.clkj.cldisabledmanclient.mvp.myrecoverorg.MyRecoverOrgDetailActivity;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的康复机构
 * Created by clboys on 2019/5/19.
 */

public class ActivityRecoverOrg extends BaseActivity implements RecoverOrgListContract.View, PullToRefreshBase.OnRefreshListener2 {


    private RelativeLayout mRlTitle;
    private ImageView mIvBack;
    private TextView mTvTitle;
    private TextView mTvRight;
    private PullToRefreshListView mLv;
    private RelativeLayout mRlEmpty;
    private TextView mTvTipEmpty;

    private RecoverOrgListContract.Presenter mPresenter;

    private ProgressDialog progressDialog;
    private List<MyRecoverOrgBean> myRecoverOrgBeanList = new ArrayList<>();
    private JayLvBaseAdapter<MyRecoverOrgBean> mAdapter;

    /**
     * 刷新标识
     */
    boolean isRefresh = false;
    /**
     * 是否可以加载更多标识
     */
    boolean canLoad = false;
    private Integer mPageIndex = 1;

    private String idCard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recover_org);
        initTitle(null, null, "康复机构", true, null);
        initData();
        initView();
        assignView();

        getRecoverOrgList();
    }

    @Override
    public void initData() {

        idCard = (String) CommonUtil.getShareValue(ShareKey.USER_IDCARD, "");
        mPresenter = new RecoverOrgListPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());

    }

    @Override
    public void assignView() {

        mAdapter = new JayLvBaseAdapter<MyRecoverOrgBean>(this, myRecoverOrgBeanList, R.layout.item_my_recoverorg) {
            @Override
            public void convert(ViewHolder helper, int position, MyRecoverOrgBean item) {

                ImageView ivOrg = helper.getView(R.id.iv_org);
                RequestOptions options = new RequestOptions().error(R.mipmap.pic_help_info_of_recover_org)
                        .placeholder(R.mipmap.ico_data_empty);
                Glide.with(mContext).load(item.getInstitutionLogo())
                        .apply(options)
                        .into(ivOrg);
                //helper.setImageByUrl(R.id.iv_org, item.getInstitutionLogo());
                helper.setText(R.id.tv_orgName, item.getInstitutionName());
                helper.setText(R.id.tv_revocerType, "康复类型：" + item.getInstitutionClass());
            }
        };

        mLv.setAdapter(mAdapter);

        //点击事件
        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent toDetail = new Intent(ActivityRecoverOrg.this, MyRecoverOrgDetailActivity.class);

                toDetail.putExtra("institutionId", myRecoverOrgBeanList.get(position - 1).getInstitutionId());
                toDetail.putExtra("personnelId", myRecoverOrgBeanList.get(position - 1).getPersonnelId());
                startActivity(toDetail);
            }
        });
    }

    @Override
    public void initView() {

        mRlTitle = findViewById(R.id.rl_title);
        mIvBack = findViewById(R.id.iv_back);
        mTvTitle = findViewById(R.id.tv_title);
        mTvRight = findViewById(R.id.tv_right);
        mLv = findViewById(R.id.lv);
        mRlEmpty = findViewById(R.id.rl_empty);
        mTvTipEmpty = findViewById(R.id.tv_tip_empty);

        mLv.setOnRefreshListener(this);
    }

    @Override
    public void getRecoverOrgList() {

        mPresenter.getRecoverOrgList(
                mPageIndex.toString(),
                Config.DEFAULT_PAGE_SIZE.toString());
    }

    @Override
    public void getRecoverOrgListSuccess(List<MyRecoverOrgBean> myRecoverOrgBeans) {

        mLv.onRefreshComplete();

        if (isRefresh) {
            myRecoverOrgBeanList.clear();
            isRefresh = false;
        }

        if (myRecoverOrgBeans.size() > 0) {

            if (myRecoverOrgBeans.size() >= Config.DEFAULT_PAGE_SIZE) {
                mLv.setMode(PullToRefreshBase.Mode.BOTH);
                canLoad = true;
            } else {
                canLoad = false;
                mLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
            }
            myRecoverOrgBeanList.addAll(myRecoverOrgBeans);

        } else {
            canLoad = false;
            mLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

            if (myRecoverOrgBeanList.size() == 0) {
                mLv.setEmptyView(mRlEmpty);
                mTvTipEmpty.setText("未查询到康复机构数据");
            } else {
                ToastUtil.showShort(this, TipStr.TIP_NOT_MORE_DATAS);
            }
        }

        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void getRecoverOrgListError(String tip) {

        mLv.onRefreshComplete();
        if (isRefresh) {
            myRecoverOrgBeanList.clear();
            isRefresh = false;
        }
        ToastUtil.showShort(this, tip);
        canLoad = false;
        mLv.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        if (myRecoverOrgBeanList.size() == 0) {
            mLv.setEmptyView(mRlEmpty);
        }
    }

    @Override
    public void setPresenter(RecoverOrgListContract.Presenter presenter) {

        this.mPresenter = presenter;
    }

    @Override
    public void showLoading() {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.setIndeterminate(false);
            progressDialog.setCancelable(true);
            //设置点击进度对话框外的区域对话框不消失
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.setMessage("正在加载...");
            progressDialog.show();
        }
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {

        mPageIndex = 1;
        getRecoverOrgList();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
        mPageIndex++;
        getRecoverOrgList();
    }
}
